<?php
defined('RUN') or die('No access');
class Model
{
    private static $a = array();
    public static function get($b)
    {
        Db::connect();
        if (!self::has($b)) {
            $c = str_replace('/', DS, $b);
            $d = MODELS_PATH . $c . EXT;
            if (is_readable($d)) {
                include $d;
                $c = ucfirst(basename($c)) . 'Model';
                self::$a[$b] = new $c();
            } else {
                error_show('Model `' . $b . '` is not found !');
            }
        }
        return self::$a[$b];
    }
    public static function has($b)
    {
        return array_key_exists($b, self::$a);
    }
    public static function remove($b)
    {
        if (self::has($b)) {
            unset(self::$a[$b]);
            return true;
        }
        return false;
    }
    public static function dump()
    {
        return array_keys(self::$a);
    }
    private static function getSelectString($e = '*')
    {
        if (is_string($e)) {
            return $e;
        } elseif (is_array($e)) {
            return '`' . implode('`, `', $e) . '`';
        }
    }
    private static function getValueString($f)
    {
        if (is_int($f)) {
            return Db::quote($f, PDO::PARAM_INT);
        } else {
            return Db::quote($f);
        }
    }
    private static function getConditionString($g = '')
    {
        if (is_string($g)) {
            return $g;
        } elseif (is_array($g)) {
            $h = array();
            if (!array_key_exists(0, $g)) {
                $h[0] = $g;
            } else {
                $h = $g;
            }
            $j = array();
            $k = count($h);
            for ($l = 0; $l < $k; $l++) {
                $m = '';
                $g = $h[$l];
                $m = ($l > 0 ? $g['type'] : '') . ' `' . $g['field'] . '` ' . (isset($g['signal']) ?
                    $g['signal'] : '=') . ' ' . self::getValueString($g['value']);
                $j[] = $m;
            }
            return implode(' ', $j);
        }
        return '';
    }
    private static function getOrderString($n = '')
    {
        if (is_string($n)) {
            return $n;
        } elseif (is_array($n)) {
            $o = array();
            if (!array_key_exists(0, $n)) {
                $o[0] = $n;
            } else {
                $o = $n;
            }
            $p = array();
            $q = count($o);
            for ($l = 0; $l < $q; $l++) {
                $r = '';
                $n = $o[$l];
                $r = '`' . $n['field'] . '` ' . (isset($n['type']) ? $n['type'] : 'ASC');
                $p[] = $r;
            }
            return implode(', ', $p);
        }
        return '';
    }
    public static function getData($s, $e = '*', $h = '', $t = 0, $u = 0, $o = '', $v = false)
    {
        $w = self::getSelectString($e);
        $m = self::getConditionString($h);
        $x = (is_int($t) && $t > 0) ? intval($t) : '';
        $y = (is_int($u) && $u > 0) ? intval($u) : '';
        $r = self::getOrderString($o);
        $z = 'SELECT ' . ($v ? 'DISTINCT ' : '') . $w . ' FROM `' . $s . '`';
        $z .= ($m == '' ? '' : ' WHERE ' . $m);
        $z .= ($r == '' ? '' : ' ORDER BY ' . $r);
        $z .= ($x == '' ? '' : ' LIMIT ' . $x);
        $z .= ($y == '' ? '' : ' OFFSET ' . $y);
        $aa = Db::query($z);
        $bb = array();
        if ($aa->rowCount() > 0) {
            $bb = Query::fetchAll($aa);
        }
        return $bb;
    }
    public static function insertData($s, $bb)
    {
        $cc = array_keys($bb);
        $dd = '(`' . implode('`, `', $cc) . '`)';
        foreach ($bb as &$f) {
            $f = Db::quote($f);
        }
        $ee = '(' . implode(', ', $bb) . ')';
        $z = "INSERT INTO `{$s}` {$dd} VALUES {$ee}";
        return Db::exec($z);
    }
    public static function updateData($s, $bb, $h)
    {
        if (count($bb) == 0) {
            return0;
        }
        $ff = array();
        foreach ($bb as $gg => $f) {
            $ff[] = '`' . $gg . '` = ' . Db::quote($f);
        }
        $m = self::getConditionString($h);
        $z = 'UPDATE `' . $s . '`';
        $z .= ' SET ' . implode(', ', $ff);
        $z .= ($m == '' ? '' : ' WHERE ' . $m);
        return Db::exec($z);
    }
    public static function deleteData($s, $h)
    {
        $m = self::getConditionString($h);
        $z = 'DELETE FROM `' . $s . '`';
        $z .= ($m == '' ? '' : ' WHERE ' . $m);
        return Db::exec($z);
    }
    public static function getDataSql($z)
    {
        $aa = Db::query($z);
        $bb = array();
        if ($aa->rowCount() > 0) {
            $bb = Query::fetchAll($aa);
        }
        return $bb;
    }
}
